<script>
	//1. 导入uni身份信息管理模块
	import uniIdPagesInit from '@/uni_modules/uni-id-pages/init.js';
	//2. 导入uniIm的Utils工具类
	import uniImUtils from '@/uni_modules/uni-im/common/utils.js';
	export default {
		onLaunch: function() {
			console.log('App Launch');
			// #ifdef MP-WEIXIN
			  this.miniAppUpdate()
			// #endif
			
			//3. 初始化uni身份信息管理模块
			uniIdPagesInit();
			//4. 初始化uniIm
			uniImUtils.init();
			
			// uni.reLaunch({
			// 	url: '/pages/index/index'
			// });
			//在线消息
			//{"type":"receive","data":{"unipush_version":"2.0","payload":{"openType":"zhaobiao","busId":"1_43_0731_69594"},"title":"全国招投标APP才能招标","content":"全国最新招标信息尽在才能招标","__UUID__":"androidPushMsg96076886","appid":"__UNI__8DA400C"}} 
			//点击离线消息
			//{"type":"click","data":{"title":"全国招投标APP才能招标","content":"\"全国最新招标信息尽在才能招标\"","unipush_version":"2.0","payload":{"openType":"zhaobiao","busId":"1_43_0731_69594"},"__UUID__":"androidPushMsg266744393","appid":"__UNI__8DA400C"}} 
			//点击本地消息
			//{"type":"click","data":{"__UUID__":"androidPushMsg7491832","title":"com.caineng.zhaobiao.app","appid":"__UNI__8DA400C","content":"全国最新招标信息尽在才能招标","payload":{"openType":"zhaobiao","busId":"1_43_0731_69594"}}} 
			 
			uni.onPushMessage((msg) => {
				console.log("收到推送消息：", msg) //监听推送消息
				
				let data = msg.data ;//监听接收到的消息
				if(msg.type=="receive"){ //在线收到的消息
				
					//创建一个本地消息
					let dataMsg={
						"title":data.title,
						"content":data.content,
						"payload":data.payload,
						"success":function(res){
							console.info('本地消息推送成功')
							// 添加红点
							uni.showTabBarRedDot({
							  index: 2
							})
						},
						"fail":function(res){
							console.info('本地消息推送失败',res)  
						}
					}
					uni.createPushMessage(dataMsg);
				}else if(msg.type=="click"){ //离线收到的消息
					console.info(data.payload.openType+"     "+data.payload.busId)
					if(data.payload.openType=='zhaobiao'){ //进招标详情
						if(data.payload.busId){
							 
							uni.navigateTo({
								url:`/pagesIndex/detail?id=${data.payload.busId}`
							})
						}else{
							 
							uni.reLaunch({
								url:`/pages/index/index`
							})
						}
					}else if (data.payload.openType=='company'){ //进公司详情
						if(data.payload.busId){
							 
							uni.navigateTo({
								url:`/pagesFind/detailenterprise/detailenterprise?companyCid=${data.payload.busId}`
							})
						}else{
							 
							uni.reLaunch({
								url:`/pages/index/index`
							})
						}
					}else{
						//如果不是这两种情况，先进首页
						uni.reLaunch({
							url:`/pages/index/index`
						})
					}
				}
				 
				
			})
			 
			

		},
		onShow: function() {
			console.log('App Show');
			this.$initPageTitle(); //初始化页面标题
		},
		onHide: function() {
			console.log('App Hide');
		},

		methods: {
			//小程序版本更新
			miniAppUpdate(){
				const updateManager = uni.getUpdateManager();
				 
				updateManager.onCheckForUpdate(function (res) {
				  // 请求完新版本信息的回调
				  console.log(res.hasUpdate);
				});
				updateManager.onUpdateReady(function (res) {
					console.log("+++");
				  uni.showModal({
					title: '才能招标-更新提示',
					content: '才能招标新版本已经准备好，是否重启应用？',
					success(res) {
					  if (res.confirm) {
						// 新的版本已经下载好，调用 applyUpdate 应用新版本并重启
						updateManager.applyUpdate();
					  }
					}
				  });
				
				});
				
				updateManager.onUpdateFailed(function (res) {
				  // 新的版本下载失败
				  // 新的版本下载失败
				  uni.showModal({
					  title: '提示',
					  content: '检查到有新版本，但下载失败，请检查网络设置',
					  success(res) {
						  if (res.confirm) {
							  // 新的版本已经下载好，调用 applyUpdate 应用新版本并重启
							  updateManager.applyUpdate();
						  }
					  }
				  });
				});
			}
		}
	};
</script>

<style lang="scss">
	
	/* #ifndef APP-NVUE */
		/*每个页面公共css */
		/* 注意要写在第一行，同时给style标签加入lang="scss"属性 */
		@import "uview-ui/index.scss";
		@import './common/uni.css';
		@import './common/iconfont.css';
		@import './common/app.scss';
		@import './common/common.css';
		@import '@/static/fonts/iconfont.css'
	/* #endif */
	
	
</style>
